package com.newenergy.light.Helper;

/**
 * 异或加密方式
 * Created by new on 2017/11/27.
 */

public class EncryUtil {
    /**
     * @param cmdData 原始的加密数据
     * @param seed    干扰种子
     * @param poly    反馈参数
     * @return
     */
    public static String encrypt(String cmdData,int seed,int poly,String deviceType) {
        if (cmdData == null) {
            return null;
        }
        byte[] bytes =BleDataHelper.StrToBytes(cmdData);;//原始数据
        int length = bytes.length;//原始数据的长度

        long shift ;//移位寄存器
        long cf ;
        long cf2 ;
        byte[] data_out = new byte[length];//加密数据
        /*long seed=0x49;//干扰种子
        long poly = 0x12A9;//反馈参数*/

        //获取发送序列
        String serial = BleDataHelper.getCurrentSerial(deviceType);

        shift = Math.abs(Long.parseLong(serial,16));

        for (int i = 0; i < length; i++) {
            bytes[i]^=seed;
            for (int j = 0; j < 8; j++) {
                cf = shift & 0x8000;
                cf >>= 15;
                shift <<= 1;
                shift&=0xffff;
                if (cf != 0) {
                    //与反馈参数做异或操作
                    shift ^= poly;
                }

                if (shift == 0) {
                    shift = 0X0425;
                }

                cf2 = bytes[i] & 0x80;
                cf2 >>= 7;

                bytes[i] <<= 1;
                cf ^= cf2;
                data_out[i] <<= 1;
                data_out[i] += cf;
            }
        }
        return BleDataHelper.bytesToHexString(data_out);
    }

    /**
     * 解密
     * @param cmdData 原始的加密数据
     * @param seed    干扰种子
     * @param poly    反馈参数
     * @return
     */
    public static String decrypt(String cmdData,int seed,int poly,String deviceType) {
        if (cmdData == null) {
            return null;
        }
        byte[] bytes =BleDataHelper.StrToBytes(cmdData);;//原始数据
        int length = bytes.length;//原始数据的长度

        long shift ;//移位寄存器
        long cf ;
        long cf2 ;
        byte[] data_out = new byte[length];//加密数据

        //获取发送序列
        String serial = "";
        if (deviceType.equals("7201")) {
            serial = BleDataHelper.getCurrentSerial(deviceType);
        }else {
            serial = deviceType;
        }


        shift = Math.abs(Long.parseLong(serial,16));

        for (int i = 0; i < length; i++) {
            bytes[i]^=seed;
            for (int j = 0; j < 8; j++) {
                cf = shift & 0x8000;
                cf >>= 15;
                shift <<= 1;
                shift&=0xffff;
                if (cf != 0) {
                    //与反馈参数做异或操作
                    shift ^= poly;
                }

                if (shift == 0) {
                    shift = 0X0425;
                }

                cf2 = bytes[i] & 0x80;
                cf2 >>= 7;

                bytes[i] <<= 1;
                cf ^= cf2;
                data_out[i] <<= 1;
                data_out[i] += cf;
            }
        }
        return BleDataHelper.bytesToHexString(data_out);
    }
}